package migrations

import (
	"database/sql"
	"time"

	"gorm.io/gorm"
)

func init() {
	AddMigration("devices", func(tx *gorm.DB) error {
		type Device struct {
			ID              int            `gorm:"primaryKey;autoIncrement"`
			TenantID        string         `gorm:"column:tenant_id;not null;index"`
			DeviceID        string         `gorm:"column:device_id;not null;uniqueIndex"`
			Type            string         `gorm:"column:type;not null;default:'sensor'"`
			Status          string         `gorm:"column:status;not null;default:'offline'"`
			Latitude        float64        `gorm:"column:latitude"`
			Longitude       float64        `gorm:"column:longitude"`
			IsGateway       bool           `gorm:"column:is_gateway;default:false"`
			ParentGatewayID sql.NullInt64  `gorm:"column:parent_gateway_id;index"`
			Metadata        sql.NullString `gorm:"column:metadata;type:jsonb"`
			CreatedAt       time.Time      `gorm:"column:created_at;default:CURRENT_TIMESTAMP"`
			UpdatedAt       time.Time      `gorm:"column:updated_at;default:CURRENT_TIMESTAMP"`
		}
		return tx.AutoMigrate(&Device{})
	})
}
